package LW.battery;
import java.util.List;

import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.collision.CollisionHandler;
import org.anddev.andengine.engine.handler.collision.ICollisionCallback;
import org.anddev.andengine.engine.handler.physics.PhysicsHandler;
import org.anddev.andengine.engine.handler.runnable.RunnableHandler;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.particle.ParticleSystem;
import org.anddev.andengine.entity.primitive.Line;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.shape.IShape;
import org.anddev.andengine.entity.shape.Shape;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.extension.ui.livewallpaper.BaseLiveWallpaperService;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.hardware.SensorManager;
import android.util.Log;
import android.view.SurfaceHolder;

public class BatteryLW extends BaseLiveWallpaperService implements SharedPreferences.OnSharedPreferenceChangeListener{
    /** Called when the activity is first created. */


	private static final int CAMERA_WIDTH = 480;
	private static final int CAMERA_HEIGHT = 800;
	
	private int i_ = 0;
	
	private static final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
	
	private TiledTextureRegion circleFaceTextureRegion_;
	
	private Line bas_, gauche_, droite_;
	private Scene scene_;
	private Body body_;
	private AnimatedSprite ball_;
	private PhysicsWorld physicsWorld_;
	private BitmapTextureAtlas bitmapTextureAtlas_;
	FixtureDef wallFixtureDef_;
	
	
	@Override
	public org.anddev.andengine.engine.Engine onLoadEngine() {
		Log.d("TAG_Battery", "onLoadEngine");
		Camera mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new org.anddev.andengine.engine.Engine(new EngineOptions(true, ScreenOrientation.PORTRAIT, new FillResolutionPolicy(), mCamera));
	}

	@Override
	public void onLoadResources() {
		Log.d("TAG_Battery", "onLoadResource");
		
		this.bitmapTextureAtlas_ = new BitmapTextureAtlas(64, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		circleFaceTextureRegion_ = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(bitmapTextureAtlas_, this, "face_circle_tiled.png", 0, 32, 2, 1); // 64x32
		this.mEngine.getTextureManager().loadTexture(bitmapTextureAtlas_);
	}

	@Override
	public void onUnloadResources() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Scene onLoadScene() {
		
		scene_ = new Scene();
		this.scene_.setBackground(new ColorBackground(0, 0, 0));
		
		physicsWorld_ = new PhysicsWorld(new Vector2(0, 9.8f), false);
		wallFixtureDef_ = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f);
		
		bas_  = new Line(50, CAMERA_HEIGHT/2, CAMERA_WIDTH-50, CAMERA_HEIGHT/2);
		gauche_  = new Line(50, CAMERA_HEIGHT/2, 50, CAMERA_HEIGHT/2-100);
		droite_  = new Line(CAMERA_WIDTH-50, CAMERA_HEIGHT/2, CAMERA_WIDTH-50, CAMERA_HEIGHT/2-100);
		
		Log.d("TAG_Battery", "onLoadScene1");
		
		body_ = PhysicsFactory.createLineBody(physicsWorld_, bas_, wallFixtureDef_);
		body_ = PhysicsFactory.createLineBody(physicsWorld_, gauche_, wallFixtureDef_);
		body_ = PhysicsFactory.createLineBody(physicsWorld_, droite_, wallFixtureDef_);

		ball_ = new AnimatedSprite(CAMERA_WIDTH/2, 0, this.circleFaceTextureRegion_);
		
		body_ = PhysicsFactory.createCircleBody(physicsWorld_, ball_, BodyType.DynamicBody, FIXTURE_DEF);
		physicsWorld_.registerPhysicsConnector(new PhysicsConnector(ball_, body_, true, true));
		
		scene_.registerUpdateHandler(this.physicsWorld_);
		
		scene_.attachChild(ball_);
		scene_.attachChild(bas_);
		scene_.attachChild(gauche_);
		scene_.attachChild(droite_);
		
		
		return scene_;
	
	}

	@Override
	protected void onTap(final int pX, final int pY)
	{
		i_ += 20;
		
		if(i_ > 200)
			i_ = 0;
		
		AnimatedSprite ball = new AnimatedSprite((CAMERA_WIDTH/2)-i_, 0, this.circleFaceTextureRegion_);
		body_ = PhysicsFactory.createCircleBody(physicsWorld_, ball_, BodyType.DynamicBody, FIXTURE_DEF);
		physicsWorld_.registerPhysicsConnector(new PhysicsConnector(ball, body_, true, true));
		scene_.attachChild(ball);
		Log.v("TAG", "onTap");
	}
	
	@Override
	public void onLoadComplete() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPauseGame() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onResumeGame() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1) {
		// TODO Auto-generated method stub
		
	}
}